package org.emergency.framework.admin;

import java.rmi.AccessException;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.emergency.framework.dao.oa.ITbPermissionDAO;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

import com.getException.webservice.saveExceptionClient;
import com.getException.webservice.saveExceptionPortType;

/**
 * 一个spring的一个例子，在这个工程里
 * 
 * @author Administrator
 * 
 */
/**
 * aop，在这里解除即可
 */
@Aspect
@Repository("likeLog")
public class LikeLog {

	// /**
	// * 方法前
	// */
	// @Before("execution(* org.emergency.framework..*(..))")
	// public void before() {
	// System.out.println("beforeMethod");
	// }
	//
	// /**
	// * 返回值后，retVal为返回值
	// *
	// * @param retVal
	// */
	// @AfterReturning(pointcut = "execution(* org.emergency.framework..*(..))",
	// returning = "retVal", value = "vl")
	// public void afterReturn(Object retVal) {
	// System.out.println("返回值为:" + retVal.toString());
	// System.out.println("afterReturn");
	// }

	/**
	 * 这个可以从joinpoint中拿到类，方法，参数,注意，最后要 return joinpoint.proceed();
	 */
	 @Around("execution(* org.emergency.framework.serviceImpl..*(..))")
	public Object aroundTheMethod(ProceedingJoinPoint joinpoint)
			throws Throwable {
		Object[] params = joinpoint.getArgs();
		System.out.println("===========================================");
		System.out.println("路径为:" + joinpoint.getSignature());
		try {
			for (int i = 0; i < params.length; i++) {
				System.out.println("第" + (i + 1) + "个参数为：" + params[i] + "，类型为："
						+ params[i].getClass());
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("这个参数为空");
		}
		System.out.println("===========================================");
		return joinpoint.proceed();
	}

	
	/**
	 * 报错的时候,这里把报错弄到getException工程里
	 * @param ex
	 */
//	@AfterThrowing(pointcut = "execution(* org.emergency.framework.serviceImpl..*(..))", 
//			throwing = "ex")
	public void doRecoveryActions(Exception ex) {
		try {
			System.out.println("这是spring afterThrowing begin");
			/**
			 * 下面的为详细的错误
			 */
			StringBuffer stringBuffer = new StringBuffer();
			StackTraceElement[] trace = ex.getStackTrace();
			for (int i = 0; i < trace.length; i++) {
				stringBuffer.append("\n\tab " + trace[i]);
			}
			/**
			 * 以为下把错误发到getException中
			 */
//		System.out.println(ex.getClass().getCanonicalName()+"**");
//		System.out.println(stringBuffer.toString()+"***");
			saveExceptionClient client = new saveExceptionClient();
			saveExceptionPortType service = client.getsaveExceptionHttpPort();
					System.out.println(service.save(ex.getClass().getCanonicalName(), stringBuffer.toString(),
					"邓国明1", "hg"));
			System.out.println("这是spring afterThrowing end");
		} catch (Exception e) {
			// TODO Auto-generated catch block
		}
	}
}
